package com.hackerrank.www.datastructures;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.StringTokenizer;

/**
 * Created by 27340 on 2017/5/6.
 */
public class SparseArrays {
    static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tok = null;
    public static void main(String[] args){
        int n = getInt();
        HashMap<String,Integer> strContains = new HashMap();
        for(int i=0;i<n;i++){
            String key = getStr();
            if(strContains.get(key)==null){
                strContains.put(key, 1);
            }else{
                strContains.put(key, strContains.get(key)+1);
            }
        }
        int q = getInt();
        for(int i=0;i<q;i++){
            String key = getStr();
            if(strContains.get(key)==null){
                System.out.println(0);
            }else{
                System.out.println(strContains.get(key));
            }
        }
    }

    static String gets() throws IOException {
        if(tok == null || !tok.hasMoreTokens())
            tok = new StringTokenizer(br.readLine());
        return tok.nextToken();
    }

    static String getStr() {
        try {
            return gets();
        } catch(IOException | NumberFormatException e) {
            return "";
        }
    }

    static int getInt() {
        try {
            return Integer.parseInt(gets());
        } catch(IOException | NumberFormatException e) {
            return -1;
        }
    }
}
